﻿using Cognex.VisionPro;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.ToolBlock;
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace Delta
{
    //public enum Vpptype {myCogToolBlock1, myCogToolBlock2 }
    //public delegate void DelegateLoad(Vpptype vpptype, string message);   
    public partial class FormVpToolBlock : Form
    {
        private Form1 form1;
        public static int xsave;
        //public static CogToolBlock myCogToolBlock1, myCogToolBlock2;
        public static event DelegateLoad VppLoad;
        public int[] Cam1Result = new int[1];
        public CogImageFileTool cogImageFile;
      
        /// <summary>
        /// 是否手动触发过相机。用于判断是否执行相机计数清空
        /// </summary>
       

        public FormVpToolBlock(Form1 main)
        {
            InitializeComponent();
            form1 = main;
            cogImageFile = new CogImageFileTool();           
          
          
        }

        private void FormVpToolBlock_Load(object sender, EventArgs e)
        {

            Form1.logger.Info("视觉设置界面已经打开");          
        }

        private void FormVpToolBlock_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.Dispose();
        }

        public string DefalutVppPath = Application.StartupPath + "\\Vpp\\";

        private void btnSaveExit1_Click_1(object sender, EventArgs e)
        {
            Cog.SaveVpp(Vpptype.myCogToolBlock1, DefalutVppPath + form1.layoutInformation.CurrentMaterial + @"\DeltaVpp.vpp");

            MessageBox.Show("保存修改", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Form1.logger.Info("已保存产品1视觉程序");
        }

        private void btnNotSaveExit1_Click_1(object sender, EventArgs e)
        {
            //data.NotSaveVpp();
            Cog.LoadVpp(DefalutVppPath + @"\" + form1.layoutInformation.CurrentMaterial + @"\", DefalutVppPath + @"\" + form1.layoutInformation.CurrentMaterial + @"\", false, Vpptype.myCogToolBlock1);            
            Form1.logger.Info("保存视觉程序操作已取消");
            MessageBox.Show("操作已取消", "确认", MessageBoxButtons.OK, MessageBoxIcon.Information);
            GC.Collect();
            this.Close();
        }

        public void ImageToolBlockRun(CogImage8Grey image, int Station)
        {
            try

            {
                SnapInfo info_Cam = new SnapInfo();
                info_Cam.Image = image;
                info_Cam.Station = Station;

                if (form1.BackWorkerInspectImage[Station].IsBusy == false)
                {
                    form1.BackWorkerInspectImage[Station].RunWorkerAsync(info_Cam);
                }

            }

            catch (Exception)
            {


            }

        }




    }
    public class SnapInfo
    {
        public CogImage8Grey Image;      

        public int CamID = 0;       
        public int Station = 0;
      
    }
    }